package controller;

import beans.Model;
import dao.ModelDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.xpath.operations.Mod;
import utils.LogOperateEnum;
import utils.LogUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * author:朱耀堃
 * 由于甲方不需要多余的字段，所以这里需要手动将字段填充  sad
 */
public class ModelList extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        boolean type = request.getParameter("type").equals("1");
        List<Object> models = new ModelDao().getAllModel(type,false);
        JSONObject result = new JSONObject();
        JSONObject content = new JSONObject();
        JSONArray resultArray = new JSONArray();
        for(int i=0;i<models.size();i++){
            Model model = (Model) models.get(i);
            JSONObject oneModel = new JSONObject();
            oneModel.put("createPerson",model.getCreatePerson());
            oneModel.put("createTime",model.getCreateTime());
            oneModel.put("introduction",model.getIntroduction());
            oneModel.put("modelId",model.getModelId());
            oneModel.put("name",model.getName());
            resultArray.add(oneModel);
        }
        content.put("models", resultArray);
        result.put("result", content);
        response.getWriter().print(result.toString());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
